package com.homwee.tvap.common.comSettingInterface;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import com.homwee.factory.FactoryActivity;
import com.homwee.factory.FactoryIniReadService;
import com.homwee.factory.R;

//zcj 2018-10-31 import org.apache.http.conn.util.InetAddressUtils;

import com.homwee.factory.R.string;
import com.homwee.factory.util.Contants;
import com.homwee.tvap.common.ap.ComSettingManager;
import com.homwee.tvap.common.srvfunc.FactorySetting;
//zcj 2018-10-30 import com.changhong.android.tvapi.common.vo.ChOsType.ENUMPIDStatus;
//import com.changhong.inface.net.NetworkSettingUtils;
//zcj 2018-10-30 import com.changhong.tvos.service.TvMiscManager;
import com.mediatek.twoworlds.factory.MtkTvFApiNetwork;
import com.mediatek.twoworlds.factory.MtkTvFApiPeripheral;
import com.mediatek.twoworlds.factory.MtkTvFApiSystem;
import com.mediatek.twoworlds.factory.common.MtkTvFApiKeyManager;
import com.mediatek.twoworlds.factory.common.MtkTvFApiResultTypeBase;
import com.mediatek.twoworlds.factory.model.MtkTvFApiSysFactorySvcCmd_e;
import com.mediatek.twoworlds.tv.MtkTvCI;
import com.mediatek.twoworlds.factory.MtkTvFApiInformation;
import com.mediatek.twoworlds.factory.common.MtkTvFApiPeripheralTypes.HDCP_ReloadType;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.SystemClock;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

//import com.changhong.tvos.common.MiscManager;
//import com.changhong.tvos.common.TVManager;
//import com.changhong.tvos.model.ChOsType.ENUMPIDStatus;

//second page
public class factoryPage2 {
	// private static MiscManager miscManager;
	// private TvMiscManager mTvMiscManager = null;
	MtkTvFApiNetwork mMtkTvFApiNetwork = null;
	MtkTvFApiSystem mMtkTvFApiSystem = null;
	MtkTvFApiPeripheral mMtkTvFApiPeripheral = null;
	MtkTvCI mMtkTvCI= null;
	WifiManager mWifiManager = null;
	String TAG = "CHFactory.factoryPage2";
	static factoryPage2 Interface = null;
	MenuConfigManager mCfg = MenuConfigManager.getInstance(null);
	private static final String BOOT_MUSIC_ANDROID_DST = "/vendor/tvconfig/boot0.mp3"; //dst path seen by Android
	private Context mContext;
	
	private final File[] DataPaths=new File[Contants.mDataPathStrings.length];

	private factoryPage2(Context context) {
		// TVManager tvManager = TVManager.getInstance(context);
		// try {
		// miscManager = tvManager.getMiscManager();
		// mTvMiscManager =TvMiscManager.getInstance();
		mContext=context;
		mMtkTvFApiNetwork = new MtkTvFApiNetwork();
		mMtkTvFApiSystem = MtkTvFApiSystem.getInstance();
		mMtkTvFApiPeripheral = MtkTvFApiPeripheral.getInstance();
		mMtkTvCI=MtkTvCI.getInstance(0);
		mWifiManager = context.getSystemService(WifiManager.class);
		// } catch (Exception e) {
		// TODO: handle exception
		// Log.v("tv", "miscManager ==null");
		// }
	}

	public static factoryPage2 getInstance(Context context) {
		if (Interface == null) {
			Interface = new factoryPage2(context);
		}
		return Interface;
	}

	public int updateCIPlusKey() {
		//int ret = 0;
		// ret = mMtkTvCI.updateCIKey();
		int encryptRt=mMtkTvCI.updateCIKeyWithPathEx(Contants.KEY_PATH_CIPLUS,0);//0 standard 1 ECP
                if (isKeyStatusIniFileExist(Contants.KEYSTATUS_CI)) {
			 setCIPlusKeyToIni(""+(encryptRt==0?"OK":("NG:"+encryptRt)));
		}

		return encryptRt;
	}

	public String getMAC() {
		/*Enumeration<NetworkInterface> interfaces = null;
		try {
			interfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		String hardWareAddress = null;
		NetworkInterface iF = null;
		if (interfaces == null) {
			return null;
		}
		while (interfaces.hasMoreElements()) {
			iF = interfaces.nextElement();
			if (iF.getDisplayName().equalsIgnoreCase("eth0")) {
				try {
					hardWareAddress = bytesToString(iF.getHardwareAddress());
					if (hardWareAddress != null)
						break;
				} catch (SocketException e) {
					e.printStackTrace();
				}
			} else {
				hardWareAddress = "";
			}
		}
		return hardWareAddress;*/

		String hardWareAddress = mMtkTvFApiSystem.getEmmcEnvVar("macaddr");
		/*String hardWareAddress = "";
		int i=0;
		while (i<=10){
			hardWareAddress=mMtkTvFApiSystem.getEmmcEnvVar("macaddr");
			if(hardWareAddress!=null){
				break;
			}
			i++;
		}*/
		Log.v(TAG, "hardWareAddress =" + hardWareAddress);
		if (hardWareAddress == null||hardWareAddress.equalsIgnoreCase("")) {
			hardWareAddress = "null";
		}
		else{
			Log.v(TAG, "hardWareAddress =" + hardWareAddress.length());
		}

		return hardWareAddress;
	}

	public String getDeviceID() {
		String devIDStr = mMtkTvFApiSystem.getEmmcEnvVar("serialno");
		Log.v(TAG, "devIDStr =" + devIDStr);
		if (devIDStr == null||devIDStr.equalsIgnoreCase("")) {
			devIDStr = "null";
		}
		else{
			Log.v(TAG, "devIDStr =" + devIDStr.length());
		}
		return devIDStr;
		
	}
	
	public String getTvWorkTime(){
		Log.d("lhtest444", "getTvWorkTime");
		String preTime=MtkTvFApiInformation.getInstance().getIniParameter(
                Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_TVWORKTIME);
		//Log.d("lhtest444", "preTime:"+preTime);
		if((!preTime.equalsIgnoreCase(""))&& (!preTime.equalsIgnoreCase("NG"))){
			//Log.d("lhtest444", " NG111 preTime:"+Long.parseLong(preTime));
			return ""+myformatDuration(Long.parseLong(preTime));//DateUtils.formatDuration(Long.parseLong(preTime));
		}else{
			//Log.d("lhtest444", " NG222");
		    return "0 minutes";
		}
	}
	
	public String myformatDuration(long millis){
		int totalSeconds = (int) (millis / 1000);
	    int minutes = (totalSeconds / 60) % 60;
	    int hours = totalSeconds / 3600;
	    String str="";
	    if((hours>0)&&(hours<2)){
	    	str+=hours+" hour";
	    }else if(hours>=2){
	    	str+=hours+" hours";
	    }
	    if(minutes>0){
	    	str+=" "+minutes+" minutes";
	    }
	    return str;
	}
	
	public void setTvWorkTime(){
		Log.d("lhtest333", "setTvWorkTime");
		String preTime=MtkTvFApiInformation.getInstance().getIniParameter(
                Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_TVWORKTIME);
		//Log.d("lhtest333", "preTime:"+preTime);
		if((!preTime.equalsIgnoreCase(""))&& (!preTime.equalsIgnoreCase("NG"))){
			
			long tvTime=Long.parseLong(preTime)+30*60*1000;
			//Log.d("lhtest333", " NG111 tvTime:"+tvTime);
			MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_TVWORKTIME, ""+tvTime);
		}else{
			//Log.d("lhtest333", " NG222");
			MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_TVWORKTIME, ""+30*60*1000);
		}
	}

	public int getPIDStatus() {
		/*
		 * //zcj 2018-10-30 ENUMPIDStatus pidStatus
		 * =ENUMPIDStatus.CH_PID_STATUS_NULL; Log.v(TAG,"getPIDStatus start");
		 * if(mTvMiscManager !=null){ pidStatus =mTvMiscManager.getPIDStatus();
		 * } Log.v(TAG,"pidStatus ="+pidStatus);
		 * 
		 * return pidStatus.ordinal();
		 */
		return 0;
	}

	public String getPID() {
		int PIDStatus = getPIDStatus();
		String pidStr = "NO PID";
		if (PIDStatus != 1) {

			/*
			 * //zcj 2018-10-30 if(mTvMiscManager !=null){ pidStr
			 * =mTvMiscManager.getPID(); }
			 */
			Log.v(TAG, "barCode =" + pidStr);

		}
		return pidStr;
	}

	public void setPID(String valueStr) {
		/*
		 * //zcj 2018-10-30 if(mTvMiscManager !=null){
		 * mTvMiscManager.setPID(valueStr); }
		 */
		Log.v(TAG, "setbarCode =" + valueStr);
	}

	public String getLocalIpAddress() {
		Log.v("tv", "getLocalIpAddress");
		Enumeration<NetworkInterface> interfaces = null;
		try {
			interfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		String hardWareAddress = null;
		NetworkInterface iF = null;
		Enumeration<InetAddress> mInetAddress=null;
		if (interfaces == null) {
			return null;
		}
		while (interfaces.hasMoreElements()) {
			iF = interfaces.nextElement();
			 mInetAddress = iF.getInetAddresses();
			while (mInetAddress.hasMoreElements()) {
				InetAddress inetAddress = (InetAddress) mInetAddress
						.nextElement();
				if(inetAddress!=null && inetAddress instanceof Inet4Address){
					  if(inetAddress.getHostAddress().equals("127.0.0.1")){
	                        continue;
					  }   
	                   hardWareAddress=inetAddress.getHostAddress();
				}
			}
			if (hardWareAddress != null)
					break;
		}
		return hardWareAddress;
	}

	public boolean isLanConnected() {
		Log.v("tv", "isLanConnected");
		Enumeration<NetworkInterface> interfaces = null;
		try {
			interfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		boolean ret = false;
		NetworkInterface iF = null;
		Enumeration<InetAddress> mInetAddress=null;
		if (interfaces == null) {
			return false;
		}
		while (interfaces.hasMoreElements()) {
			iF = interfaces.nextElement();
			if (iF.getDisplayName().equalsIgnoreCase("eth0")) {
				mInetAddress = iF.getInetAddresses();
				while (mInetAddress.hasMoreElements()) {
					InetAddress inetAddress = (InetAddress) mInetAddress
						.nextElement();
					if(inetAddress!=null){
	                   ret=true;
					}
				}
			}
		}
		return ret;
	}
	public String getLanIpAddress() {
		Log.v("tv", "getLanIpAddress");
		// zcj 2018-10-31 NetworkSettingUtils mNetworkSettingUtils =
		// NetworkSettingUtils.getInstance(null);
		// zcj 2018-10-31 return mNetworkSettingUtils.getIPAddress();
		return "null";
	}

	public String getIPAdress() {
		return getLocalIpAddress();
		// return getLanIpAddress();
	}

	public int getScreenCS() {
		return 0;// mTvMiscManager.getPdpLogicUpdateChecksum();
	}

	public void setNetDetect() {

	}

	public boolean setMAC(String mac) {
		int mode=-1;
		if (mMtkTvFApiSystem != null){
			mode=mMtkTvFApiSystem.setEmmcEnvVar("macaddr", mac);
		}

		if(mode>-1){
			int command = MtkTvFApiSysFactorySvcCmd_e.E_MTK_TV_FAPI_SYS_FACTORY_CMD_SYS_CALL;
			String parameter = "ifconfig eth0 down";
			MtkTvFApiSystem.getInstance().sendCmdToFactorySvc(command, parameter);
			String parameter_1 = "ifconfig eth0 hw ether "+mac;
			MtkTvFApiSystem.getInstance().sendCmdToFactorySvc(command, parameter_1);
			String parameter_2 = "ifconfig eth0 up";
			MtkTvFApiSystem.getInstance().sendCmdToFactorySvc(command, parameter_2);
		}
		return (mode>-1);
	}

	public boolean deleteFileByCmd(String path){
		int mode=-1;
		int command = MtkTvFApiSysFactorySvcCmd_e.E_MTK_TV_FAPI_SYS_FACTORY_CMD_SYS_CALL;
		//String parameter = "rm -rf "+path;
		String parameter = "rmdir "+path;
		Log.d("Arthur cmd",parameter);
		mode=mMtkTvFApiSystem.sendCmdToFactorySvc(command, parameter);
		Log.d("Arthur mode",mode+"");
		return (mode>-1);
	}
	public void setUSBUpdateMAC() {

	}

	public boolean setDeviceID(String deviceID) {
		int mode=-1;
		if (mMtkTvFApiSystem != null){
			mode=mMtkTvFApiSystem.setEmmcEnvVar("serialno", deviceID);
		}
		return (mode>-1);
	}

	public boolean setBackupDataToUSB(byte[] buffer, int length) {
		Log.v("tv", "setBackupDataToUSB length =" + length);
		for (int i = 0; i < length; i++) {
			Log.v("tv", "setBackupDataToUSB buffer =" + buffer[i]);
		}
		boolean value = false;
		Log.v("tv", "setBackupDataToUSB return value =" + value);
		return value;
	}

	public void setScreenUpgrade() {

	}

	public boolean updateCurrentPaneldata() {
		// TODO Auto-generated method stub
		// zcj 2018-10-30 boolean value
		// =mTvMiscManager.updateCurrentPaneldata();
		// zcj 2018-10-30
		// Log.v("tv","updateCurrentPaneldata return value ="+value);
		// zcj 2018-10-30 return value;
		return false;
	}

	public boolean Update6M40bindata(byte[] bytes, int length) {
		// TODO Auto-generated method stub
		// return true;//miscManager.Update6M40bindata(bytes, length);
		Log.v("tv", "Update6M40bindata length =" + length);
		for (int i = 0; i < length; i++) {
			Log.v("tv", "Update6M40bindata buffer =" + bytes[i]);
		}
		// zcj 2018-10-30 boolean value =mTvMiscManager.update6M40bindata(bytes,
		// length);
		// zcj 2018-10-30 Log.v("tv","Update6M40bindata return value ="+value);
		// zcj 2018-10-30 return value;
		return false;
	}

	public boolean setHdcpKey2x(String src) {
		// TODO Auto-generated method stub
		Log.v("tv", "writeEepDataByName hdcp_2.0");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_HDCP2, false);

		if(copyRt > -1){
			int chmod=changeFileMode(0644,Contants.KEY_PATH_HDCP2);
			if(chmod!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_HDCPKEY2)){
				setHdcpKey2xToIni("NG:changFileMode "+chmod);
			}
			int encryptRt = mMtkTvFApiSystem
					.encryptSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_HDCP2,true);
			reloadNeedKeys();
			if(encryptRt!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_HDCPKEY2)){
				setHdcpKey2xToIni("NG:encrypt "+encryptRt);
				return encryptRt > -1;
			}
		}
		else{
			if(isKeyStatusIniFileExist(Contants.KEYSTATUS_HDCPKEY2)){
				setHdcpKey2xToIni("NG:copyFile "+copyRt);
			}
		}

		return copyRt > -1;
	}

	public boolean setNetflixKey(String src) {
		// TODO Auto-generated method stub
		Log.v("tv", "setNetflixKey");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_NETFLIX_KEY, false);
		if (copyRt > -1) {
			int chmod = changeFileMode(0644, Contants.KEY_PATH_NETFLIX_KEY);
			if (chmod != 0 && isKeyStatusIniFileExist(Contants.KEYSTATUS_NETFLIX)) {
				setNetflixKeyToIni("NG:changeFileMode " + chmod);
			}
		} else {
			if (isKeyStatusIniFileExist(Contants.KEYSTATUS_NETFLIX)) {
				setNetflixKeyToIni("NG:copyFile " + copyRt);
			}
		}
		int encryptRt = mMtkTvFApiSystem
				.encryptSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_NETFLIX50, true);
		if (encryptRt != 0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_NETFLIX)) {
			setNetflixKeyToIni("NG:encrypt " + encryptRt);
		}
		return (copyRt > -1);
	}

    public boolean setNetflixESNKey(String src) {
        // TODO Auto-generated method stub
        Log.v("tv", "setNetflixESNKey ");
        int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
                Contants.KEY_PATH_NETFLIX_ESN_KEY, false);
        if(copyRt>-1){
			int chmodF=changeFileMode(0644,Contants.KEY_PATH_NETFLIX_ESN_KEY);
			int chmodD=changeFileMode(0755,Contants.KEY_PATH_NETFLIX_DIR);
			if(!(chmodD==0&&chmodF==0)&&isKeyStatusIniFileExist(Contants.KEYSTATUS_NETFLIX_ESN_KEY)){
				setNetflixESNKeyToIni("NG:changeFileMode "+chmodD+";"+chmodF);
			}
		}
        return (copyRt > -1);
    }
	public boolean setHdcpKey1x(String src) {
		Log.v("tv", "writeEepDataByName hdcp_1.4");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_HDCP14, false);

		if(copyRt > -1){
			int chmod=changeFileMode(0644,Contants.KEY_PATH_HDCP14);
			if(chmod!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_HDCPKEY14)){
				setHdcpKey1xToIni("NG:changeFileMode "+chmod);
			}
			int encryptRt= mMtkTvFApiSystem.
					encryptSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_HDCP14,false);
			reloadNeedKeys();
			if(encryptRt!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_HDCPKEY14)){
				setHdcpKey1xToIni("NG:encrypt "+encryptRt);
			}
		}
		else{
			if(isKeyStatusIniFileExist(Contants.KEYSTATUS_HDCPKEY14)){
				setHdcpKey1xToIni("NG:copyFile "+copyRt);
			}
		}
		return (copyRt > -1);
	}
	public boolean setHashKey(String src) {
		Log.v("tv", "writeEepDataByName setHashKey");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_HASH_KEY, false);
		return (copyRt > -1);
	}

	/**
	 *
	 * @param src path
	 * @return success?
	 */
	public boolean setProjectId(String src) {
		return mMtkTvFApiSystem.setProjectID(src)==1?true:false;
	}
	public boolean setPlayReadyKey_1(String src) {
		Log.v("tv", "setPlayReadyKey");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_PLAYREADY_1, false);
		if(copyRt>-1){
			int chmRt=changeFileMode(0666,Contants.KEY_PATH_PLAYREADY_1);
			if(chmRt!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY_1)){
				setPlayReadyKey_1ToIni("NG:changeFileMode "+chmRt);
			}
		}
		else{
			if(isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY_1)){
				setPlayReadyKey_1ToIni("NG:copyFile "+copyRt);
			}
		}
		int encryptRt = mMtkTvFApiSystem
				.encryptSystemKey(
						MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_PLAYREADY30,
						true);
		if(encryptRt!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY_1)){
			setPlayReadyKey_1("NG:encrypt "+encryptRt);
		}
		return (copyRt > -1);
	}
	public boolean setPlayReadyKey_2(String src) {
		Log.v("tv", "setPlayReadyKey ");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_PLAYREADY_2, false);
		if(copyRt>-1){
			int chmodF=changeFileMode(0666,Contants.KEY_PATH_PLAYREADY_2);
			int chmodD=changeFileMode(0755,Contants.KEY_PATH_PLAYREADY_DIR);
			if(!(chmodF==0&&chmodD==0)&&isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY_2)){
				setPlayReadyKey_2ToIni("NG:changemode: "+chmodF+";"+chmodD);
			}
		}
		else{
			if(isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY_2)){
				setPlayReadyKey_2ToIni("NG:copyFile "+copyRt);
			}
		}
		int encryptRt = mMtkTvFApiSystem
				.encryptSystemKey(
						MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_PLAYREADYPUB30,
						false);
		if(encryptRt!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY_2)){
			setPlayReadyKey_2ToIni("NG:encrypt "+encryptRt);
		}
		return (copyRt > -1);
	}
/*	
	public boolean setPlayReadyKey25_1(String src) {
		Log.v("tv", "setPlayReadyKey25");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_PLAYREADY25_1, false);
		if(copyRt>-1){
			int chmodRt=changeFileMode(0666,Contants.KEY_PATH_PLAYREADY25_1);
			if(chmodRt!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY25_1)){
				setPlayReadyKey25_1ToIni("NG:changeFileMode "+chmodRt);
			}
		}
		else{
			if(isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY25_1)){
				setPlayReadyKey25_1ToIni("NG:copyFile "+copyRt);
			}
		}
		int encryptRt = mMtkTvFApiSystem
				.encryptSystemKey(
						MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_PLAYREADY25,
						true);
		if(encryptRt!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY25_1)){
			setPlayReadyKey25_1ToIni("NG:encrypt "+encryptRt);
		}
		return (copyRt > -1);
	}
	public boolean setPlayReadyKey25_2(String src) {
		Log.v("tv", "setPlayReadyKey25 ");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_PLAYREADY25_2, false);
		if(copyRt>-1){
			int chmodF=changeFileMode(0666,Contants.KEY_PATH_PLAYREADY25_2);
			int chmodD=changeFileMode(0755,Contants.KEY_PATH_PLAYREADY25_DIR);
			if(isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY25_2)&&!(chmodF==0&&chmodD==0)){
				setPlayReadyKey25_2ToIni("NG:changeFileMode "+chmodF+";"+chmodD);
			}
		}
		else{
			if(isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY25_2)){
				setPlayReadyKey25_2ToIni("NG:copyFile"+copyRt);
			}
		}
		int encryptRt = mMtkTvFApiSystem
				.encryptSystemKey(
						MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_PLAYREADYPUB25,
						false);
		if(encryptRt!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_PLAYREADY25_2)){
			setPlayReadyKey25_2ToIni("NG:encrypt "+encryptRt);
		}
		return (copyRt > -1);
	}
*/	
	public boolean setCLIENTCAKey(String src) {
		// TODO Auto-generated method stub
		Log.v("tv", "writeEepDataByName CLIENTCAKey ");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_WEBPLATFORM_CA, false);
		if(copyRt>-1){
			int chmodF=changeFileMode(0775,Contants.KEY_PATH_WEBPLATFORM_CA);
			int chmodD=changeFileMode(0755,Contants.KEY_PATH_WEBPLATFORM_CA_DIR);
			if(!(chmodF==0&&chmodD==0)&&isKeyStatusIniFileExist(Contants.KEYSTATUS_CAKEY)){
				setCLIENTCAKey("NG:changeFileMode "+chmodF+";"+chmodD);
			}
		}
		else{
			setCLIENTCAKey("NG:copyFile "+copyRt);
		}
		return (copyRt > -1) ;

	}

	public boolean setWidevineKey(String src) {
		// TODO Auto-generated method stub
		Log.v("tv", "writeEepDataByName windvine ");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_WINDVINDE, false);
		if(copyRt>-1){
			int chmodF=changeFileMode(0644,Contants.KEY_PATH_WINDVINDE);
			int chmodD=changeFileMode(0755,Contants.KEY_PATH_WINDVINDE_DIR);
			if(!(chmodF!=0&&chmodD!=0)&&isKeyStatusIniFileExist(Contants.KEYSTATUS_WIDEVINE)){
				setWidevineKeyToIni("NG:changeFileMode: "+chmodF+";"+chmodD);
			}
		}
		else{
			if(isKeyStatusIniFileExist(Contants.KEYSTATUS_WIDEVINE)){
				setWidevineKeyToIni("NG:copyFile "+copyRt);
			}
		}
		int encryptRt = mMtkTvFApiSystem
				.encryptSystemKey(
						MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_WVCENC,
						true);
		if(encryptRt!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_WIDEVINE)){
			setWidevineKeyToIni("NG:encrypt "+encryptRt);
		}
		return (copyRt > -1) ;

	}

	public boolean setCIPlusKey(String src) {
		// TODO Auto-generated method stub
		Log.v("tv", "writeEepDataByName CI + key ");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_CIPLUS, false);
		if(isKeyStatusIniFileExist(Contants.KEYSTATUS_CI)&&copyRt!=0){
			setCIPlusKeyToIni("NG:copyFile: "+copyRt);
			return false;
		}
		int encryptRt=mMtkTvCI.updateCIKeyWithPathEx(Contants.KEY_PATH_CIPLUS,0);//0 standard 1 ECP
		/*int encryptRt = mMtkTvFApiSystem
				.encryptSystemKey(
						MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_CIPLUS,
						true);*/
       if (isKeyStatusIniFileExist(Contants.KEYSTATUS_CI)) {
    	   if(encryptRt!=0){
    		   setCIPlusKeyToIni("NG:encrypt "+encryptRt);
    	   }
		}
                
		return (copyRt > -1) && (encryptRt == 0);
	}

	public boolean setKeyMaster(String src) {
		// TODO Auto-generated method stub
		Log.v("tv", "writeEepDataByName hdcp_1.0");
		int copyRt = MtkTvFApiSystem.getInstance().copyFile(src,
				Contants.KEY_PATH_KEYMASTER, false);
		if(copyRt!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_KEYMASTER)){
			setKeyMasterToIni("NG:copyFile "+copyRt);
		}
		int encryptRt = mMtkTvFApiSystem
				.encryptSystemKey(
						MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_KEYMASTER,
						true);
		if(encryptRt!=0&&isKeyStatusIniFileExist(Contants.KEYSTATUS_KEYMASTER)){
			setKeyMasterToIni("NG:encrypt "+encryptRt);
		}
		return (copyRt > -1) ;
	}

	public void getWhiteBalanceData_Standard(byte[] key) {
		// TODO Auto-generated method stub
		Log.v("tv", "getWhiteBalanceData_Standard");
		// zcj 2018-10-30 mTvMiscManager.readEepDataByName("colortemp", key);
		for (int i = 0; i < key.length; i++) {
			Log.v("tv", "Standardkey[" + i + "] =" + key[i]);
		}
	}

	public void getWhiteBalanceData_Warm(byte[] key) {
		// TODO Auto-generated method stub
		Log.v("tv", "getWhiteBalanceData_Warm");
		// zcj 2018-10-30 mTvMiscManager.readEepDataByName("colortemp_warm",
		// key);
		for (int i = 0; i < key.length; i++) {
			Log.v("tv", "Warmkey[" + i + "] =" + key[i]);
		}
	}

	public void getWhiteBalanceData_Cool(byte[] key) {
		// TODO Auto-generated method stub
		Log.v("tv", "getWhiteBalanceData_Cool");
		// zcj 2018-10-30 mTvMiscManager.readEepDataByName("colortemp_cool",
		// key);
		for (int i = 0; i < key.length; i++) {
			Log.v("tv", "Coolkey[" + i + "] =" + key[i]);
		}
	}

	public String getLightSensorValue() {
		// TODO Auto-generated method stub
		Log.v("tv", "getLightSensorValue");
		int retInt = 0;
		// zcj 2018-10-30 retInt=mTvMiscManager.getLightSensorALSData();
		return retInt + " ";

	}

	public boolean deleteFileByPath(String path) {
		Log.v("tv", "deleteFileByPath"+path);
		boolean retbol=false;
		retbol=mMtkTvFApiSystem.removeFile(path)==0;
		return retbol;
	}

	public boolean deleteDir(String path) {
		Log.v("tv", "deleteFileByPath"+path);
		boolean retbol=false;
		retbol=mMtkTvFApiSystem.removeFile(path)==0;
		return retbol;
	}

    public boolean deleteDirByPath(String path) {
        Log.v("tv", "deleteFileByPath"+path);
        boolean retbol=false;
        File mflie=new File(path);
		Log.d("Arthur2",path);
        if(mflie.isFile()) {
            retbol = mMtkTvFApiSystem.removeFile(path) == 0;
        }else if(mflie.isDirectory()){
        	File[] tempFiles=mflie.listFiles();
        	if(tempFiles.length>0){
				for (int i = 0; i <tempFiles.length ; i++) {
					File tempfile=tempFiles[i];
					deleteDirByPath(tempfile.getAbsolutePath());
				}
				//deleteFileByPath(path);
				//boolean ret=mflie.delete();
				retbol=deleteFileByCmd(path);
				Log.d("Arthur",retbol?"success":"Fail");
        	}else{
				retbol=deleteFileByCmd(path);
				Log.d("Arthur",retbol?"success":"Fail");
			}

        }else{
		}
        return retbol;
    }
	
	
	public boolean copyFileByPath(String src,String des){
		return mMtkTvFApiSystem.copyFile(src, des, false)>-1;
	}

	public boolean copyFileSupport(File[] FileList,File desFilePath){
		for (File file : FileList) {
			if(file.isFile()) {
				copyFileByPath(
						file.getAbsolutePath(),
						desFilePath.getPath()
								+ File.separator
								+ file.getName());
			}else if(file.isDirectory()){
				File[] dirFileList=file.listFiles();
				for(File dirFile:dirFileList){
					copyFileByPath(
					dirFile.getAbsolutePath(),
							desFilePath.getPath()+File.separator+file.getName()+File.separator+dirFile.getName());
				}
			}

		}
		return true;
	}
	
	/**
	 * 
	 * @param bytes
	 * @return
	 */
	private static String bytesToString(byte[] bytes) {
		if (bytes == null || bytes.length == 0) {
			return null;
		}
		StringBuilder buf = new StringBuilder();
		for (byte b : bytes) {
			buf.append(String.format("%02X:", b));
		}
		if (buf.length() > 0) {
			buf.deleteCharAt(buf.length() - 1);
		}
		return buf.toString();
	}


	private static String callCmd(String cmd, String filter) {
		String result = "";
		String line = "";
		try {
			Process proc = Runtime.getRuntime().exec(cmd);
			InputStreamReader is = new InputStreamReader(proc.getInputStream());
			BufferedReader br = new BufferedReader(is);
			if(filter.length()>0){
				while ((line = br.readLine()) != null
						&& line.contains(filter) == false) {
					result += line;
				}
			}

			result = line;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	private static String callCmd(String cmd) {
		String result = "";
		String line = "";
		try {
			Process proc = Runtime.getRuntime().exec(cmd);
			InputStreamReader is = new InputStreamReader(proc.getInputStream());
			BufferedReader br = new BufferedReader(is);
			while ((line = br.readLine()) != null) {
				result += line;
			}
			result = line;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 *
	 * @param path
	 * @return Boolean
	 */
	public boolean changeBootLogoByPath(String path){
		int ret=-1;
		if (path.endsWith("boot0.mp3")) {
			ret = mMtkTvFApiSystem.copyFile(path, BOOT_MUSIC_ANDROID_DST, false);
			if (ret == -1) {
				Log.e(TAG, "changeBootMusic Failed!!, bootmusicFilePath: " + path + ", ret: " + ret);
            	return false;
			}
			ret = mMtkTvFApiSystem.setEmmcEnvVar("db_table", "0");
			if (ret == -1) {
				Log.e(TAG, "changeBootMusic Failed!!, bootmusicFilePath: " + path + ", ret: " + ret);
            	return false;
			}
			return true;
		}
		ret=mMtkTvFApiSystem.changeBootlogo(path);
		return ret==0?true:false;
	}

	public int reloadNeedKeys(){
		int ret=0;
		if(mMtkTvFApiSystem
				.checkSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_HDCP14) ||
                mMtkTvFApiSystem.checkSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_HDCP2)){
			ret=mMtkTvFApiPeripheral.reloadHdcpKey(HDCP_ReloadType.ALL);
			Log.d(TAG,"reloadNeedKeys:"+ret);
		} 		
		return ret;
	}

	public int changeFileMode(int mode,String path){
		int ret=-1;
		ret=mMtkTvFApiSystem.changeFileMode(path,mode);
		return ret;
	}
	
	public boolean setMacToIni (String mac) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_MAC,
		  ""+mac) != -1?true:false;
	}
	
	public String getMACFromIni() {
		return MtkTvFApiInformation.getInstance().
				getIniParameter(Contants.KEYSTATUS_INI_FILEPATH,Contants.KEYSTATUS_MAC);
	}
	
	public boolean setDeviceIDToIni (String DeviceID) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_DEVICEID,
		  ""+DeviceID) != -1?true:false;
	}
	
	public String getDeviceIDFormIni() {
		return MtkTvFApiInformation.getInstance().
				getIniParameter(Contants.KEYSTATUS_INI_FILEPATH,Contants.KEYSTATUS_DEVICEID);
	}
	
	public boolean setHdcpKey1xToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_HDCPKEY14,
		  ""+src) != -1?true:false;
	}
	
	public boolean setWidevineKeyToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_WIDEVINE,
		  ""+src) != -1?true:false;
	}
	
	public boolean setKeyMasterToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_KEYMASTER,
		  ""+src) != -1?true:false;
	}
	
	public boolean setPlayReadyKeyToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_PLAYREADY,
		  ""+src) != -1?true:false;
	}
	public boolean setPlayReadyKey_1ToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_PLAYREADY_1,
		  ""+src) != -1?true:false;
	}
	public boolean setPlayReadyKey_2ToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_PLAYREADY_2,
		  ""+src) != -1?true:false;
	}
	public boolean setHashKeyToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_HASHKEY,
		  ""+src) != -1?true:false;
	}
	
	public boolean setHdcpKey2xToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_HDCPKEY2,
		  ""+src) != -1?true:false;
	}
	
	public boolean setNetflixKeyToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_NETFLIX,
		  ""+src) != -1?true:false;
	}
	
	public boolean setNetflixESNKeyToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_NETFLIX_ESN_KEY,
		  ""+src) != -1?true:false;
	}
	
	public boolean setCIPlusKeyToIni (String src) {
		boolean result = false;
		result = MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_CI,
		  ""+src) != -1?true:false;
		return result;
	}

	public boolean isKeyStatusIniFileExist(String key) {
		boolean ret = mMtkTvFApiSystem.isFileExist(Contants.KEYSTATUS_INI_FILEPATH);
		String keyStr=null;
		if (ret) {
			keyStr = MtkTvFApiInformation.getInstance().getIniParameter(Contants.KEYSTATUS_INI_FILEPATH, key);
			if (keyStr.isEmpty() || keyStr == null) {
				ret = false;
			}
		}
		return ret;
	}
	
	public boolean isKeyStatusIniFileExist() {
		boolean ret = mMtkTvFApiSystem.isFileExist(Contants.KEYSTATUS_INI_FILEPATH);
		return ret;
	}
	/*
	public boolean setPlayReadyKey25ToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_PLAYREADY25,
		  ""+src) != -1?true:false;
	}
	public boolean setPlayReadyKey25_1ToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_PLAYREADY25_1,
		  ""+src) != -1?true:false;
	}
	public boolean setPlayReadyKey25_2ToIni (String src) {
		return MtkTvFApiInformation.getInstance()
		  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH, Contants.KEYSTATUS_PLAYREADY25_2,
		  ""+src) != -1?true:false;
	}
	*/
	public boolean setPIDToIni(String src){
		return MtkTvFApiInformation.getInstance()
				  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH,Contants.KEYSTATUS_PID,
						  ""+src)!=-1?true:false;
	}
	public boolean setCLIENTCAKeyToIni(String src){
		return MtkTvFApiInformation.getInstance()
				  .setIniParameter(Contants.KEYSTATUS_INI_FILEPATH,Contants.KEYSTATUS_CAKEY,
						  ""+src)!=-1?true:false;
	}
	
	public boolean isUpgradeKeyFileExist(){
		boolean isFileExist = false;
		isFileExist = mMtkTvFApiSystem.isFileExist(Contants.KEYUPGRADE_FILEPATH);
		return isFileExist;
	}
	
	public boolean saveUpgradeKeyFileName(String keyName, String str) {
		boolean ret = false;
		boolean isFileExist = false;
		isFileExist = isUpgradeKeyFileExist();
		if (isFileExist) {
			switch (keyName.toUpperCase()) {
			case "MAC": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_MAC, "" + str) != -1 ? true : false;
				break;
			}
			case "DEVICEID": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_DEVICEID, "" + str) != -1 ? true : false;
				break;
			}
			case "HDCPKEY14": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_HDCPKEY14, "" + str) != -1 ? true : false;
				break;
			}
			case "WIDEVINE": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_WIDEVINE, "" + str) != -1 ? true : false;
				break;
			}
			case "KEYMASTER": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_KEYMASTER, "" + str) != -1 ? true : false;
				break;
			}
			case "PLAYREADY_1": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_PLAYREADY_1, "" + str) != -1 ? true : false;
				break;
			}
			case "PLAYREADY_2": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_PLAYREADY_2, "" + str) != -1 ? true : false;
				break;
			}
			case "HASHKEY": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_HASHKEY, "" + str) != -1 ? true : false;
				break;
			}
			case "HDCPKEY2": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_HDCPKEY2, "" + str) != -1 ? true : false;
				break;
			}
			case "NETFLIX": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_NETFLIX, "" + str) != -1 ? true : false;
				break;
			}
			case "NETFILX_ESN": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_NETFLIX_ESN_KEY, "" + str) != -1 ? true : false;
				break;
			}
			case "CI": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_CI, "" + str) != -1 ? true : false;
				break;
			}
			
			case "CAKEY": {
				ret = MtkTvFApiInformation.getInstance().setIniParameter(Contants.KEYUPGRADE_FILEPATH,
						Contants.KEYSTATUS_CAKEY, "" + str) != -1 ? true : false;
				break;
			}
			default: {
				return ret;
			}
			}
			return ret;
		} else {
			return false;
		}

	}

	public void WriteProjectIDToEeprom(){
		Intent intent = new Intent();
		intent.setPackage("com.mediatek.tv.factorykey");
		intent.setAction("com.mediatek.tv.factorykey.WriteToEeprom");
		String[] types = new String[]{"PROJECT_ID"};
		intent.putExtra("TYPE", types);
		mContext.startService(intent);
	}

	public void customerUpdate(Context mContext) {
		ComSettingManager mcomSettingManager = ComSettingManager.getInstance(mContext);
		FactorySetting factorySetting = mcomSettingManager.getFactory();

		if (factorySetting.findUsbDir()) {
			final String mDataName = Contants.FACTORY_PAGE2_DATA_UPDATE_PATH;
			Log.d(TAG, mDataName);
			File mDataDirFile = factorySetting.getFile(mDataName);
			if (!mDataDirFile.exists()) {
				Toast toast = Toast.makeText(mContext,
						"USB Root Directory  Can't find "
								+ mDataName + " directory",
						Toast.LENGTH_SHORT);
				toast.show();
			} else {
				Map<String, Object> mDataIndex = FactoryIniReadService.readIni(factorySetting.getDataIndex());
				Map<String, String> mSimpleDataIndex = new HashMap<>();
				String mCurDataindex="dataIndex_" + factorySetting.getCurProjectIndex()+ ".ini";
				if (mDataIndex == null || mDataIndex.size() == 0) {
					Log.d(TAG, "mDataIndex is Null ,Path=" + factorySetting.getDataIndex());
					Toast toast = Toast.makeText(mContext,
							"read dataIndex error", Toast.LENGTH_SHORT);
				} else {
					for (String k : mDataIndex.keySet()) {
						mSimpleDataIndex.putAll((Map<String, String>) mDataIndex.get(k));
					}
					for (File f : mDataDirFile.listFiles()) {
						//dataindex has the files
						if (f.isDirectory() && !TextUtils.isEmpty(mSimpleDataIndex.get(f.getName()))) {
							File checkFile=new File(mSimpleDataIndex.get(f.getName()));
							if(checkFile.isFile()){
								for (File updateFiles : f.listFiles()) {
									factorySetting.copyFile(updateFiles.getAbsolutePath(), mSimpleDataIndex.get(f.getName()));
									Log.d(TAG,"copyFile:"+updateFiles.getAbsolutePath()+"  targetFile:"+mSimpleDataIndex.get(f.getName()));
								}
							}else{
								//is directory,add fileName after path
								for (File updateFiles : f.listFiles()) {
									factorySetting.copyFile(updateFiles.getAbsolutePath(),
											(mSimpleDataIndex.get(f.getName())+File.separator+updateFiles.getName()));
									Log.d(TAG,"copyFile:"+updateFiles.getAbsolutePath()+"  targetFile:"+(mSimpleDataIndex.get(f.getName())+File.separator+updateFiles.getName()));
								}
							}
							Toast toast = Toast
									.makeText(
											mContext,
											"Upgrade success  reboot to take effect",
											Toast.LENGTH_LONG);
							toast.show();
						}else{
							//can't find the filename in datainex
							//update dataindex
							if(f.isFile() && f.getName().equals(mCurDataindex)){
								factorySetting.copyFile(f.getAbsolutePath(),
										Contants.FACTORY_DATAINDEX_PATH+mCurDataindex);
								factorySetting.initDataPath(factorySetting.getDataIndex());
								Log.d(TAG,"copyFile:"+f.getAbsolutePath()+"  targetFile:"+Contants.FACTORY_DATAINDEX_PATH+mCurDataindex);
								Toast toast=Toast.makeText(mContext,"Update dataIndex success",Toast.LENGTH_LONG);
								toast.show();
							}else {
								Toast toast = Toast
										.makeText(
												mContext,
												"can't find the filename in datainex,please check the file name",
												Toast.LENGTH_LONG);
								toast.show();
							}
						}
					}
				}
				MtkTvFApiSystem.getInstance().setEmmcEnvVar("db_table", "0");
				//this part to make reboot
				// PowerManager pManager = (PowerManager) mContext
				//         .getSystemService(mContext.POWER_SERVICE);
				// pManager.reboot("");
			}
		}
	}
}
